package org.yty.travelagencyredis.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencyredis.model.DTO.HistoryMessageQueryDTO;
import org.yty.travelagencyredis.model.Entity.Message;
import org.yty.travelagencyredis.model.Entity.Result;
import org.yty.travelagencyredis.service.MessageService;


import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {
    private final MessageService messageService;
    @GetMapping("/{senderId}/{receiverId}")
    public Result<List<Message>> getMessages(@PathVariable Long senderId, @PathVariable Long receiverId){
        return Result.ok(messageService.getMessages(senderId,receiverId));
    }

    @PostMapping
    public Result<Void> sendMessage(@RequestBody Message message){
        message.setCreateTime(LocalDateTime.now());
        this.messageService.save(message);
        return Result.ok();
    }

    @GetMapping("/history")
    public Result<List<Message>> getHistoryMessages(@ModelAttribute HistoryMessageQueryDTO historyMessageQueryDTO){
        List<Message> result = this.messageService.getHistoryMessages(historyMessageQueryDTO);
        if (result == null){
            return Result.error("没有更多消息了");
        }
        return Result.ok(result);
    }

}
